<?php
namespace app\admin\controller;
\think\Loader::import('controller/Jump', TRAIT_PATH, EXT);
use app\admin\Controller;
use think\Db;
use think\Lang;
use think\Loader;
use think\Exception;

class Cancelinfo extends Controller
{
	use \app\admin\traits\controller\Controller;	
//	use \app\admim\traits\controller\Controller;
	use \traits\controller\Jump;
    /**
     * 列表
     */     
    public function index()
    {
		$map['c.isdelete'] = 0;
		$model = $this->getModel('Cancelinfo');
        $listRows = 10;
        $field = "i.content,i.photo,i.address,i.create_time,i.go_time,i.cate_id,c.id,c.create_time as c_time,c.cancel_status,c.reason";
      // 分页查询
        $list = $model->alias('c')
        		->join('tp_info i','c.active_id = i.id','left')
        		->field($field)
        		->where($map)
        		->order('i.create_time desc')
        		->paginate($listRows, false, ['query' => $this->request->get()]);
        // 模板赋值显示
    	foreach($list as $k => $v){
    		$data[$k]['id'] = $v['id'];
        	$data[$k]['content'] = $v['content'];
        	$data[$k]['photo'] = $v['photo'];
        	$data[$k]['address'] = $v['address'];
        	$data[$k]['count'] = $v['count'];
        	$data[$k]['create_time'] = date('Y-m-d h:i:s',$v['create_time']);
        	$data[$k]['go_time'] = date('Y-m-d h:i:s',$v['go_time']);
        	$data[$k]['cate_id'] = $v['cate_id'];
        	$data[$k]['c_time'] = date('Y-m-d h:i:s',$v['c_time']);
        	$data[$k]['reason'] = $v['reason'];
        	$data[$k]['cancel_status'] = $v['cancel_status'];
        }
        $this->view->assign('list', $data);
        $this->view->assign("page", $list->render());
        $this->view->assign("count", $list->total());
        $this->view->assign('numPerPage', $listRows);
			
        return $this->view->fetch();
    }

	/**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        $controller = $this->request->controller();
        $module = $this->request->module();

        if ($this->request->isAjax()) {
            // 更新
            $data = $this->request->post();
            
            if (!$data['id']) {
                return ajax_return_adv_error("缺少参数ID");
            }
                       
            // 验证
            if (class_exists(Loader::parseClass($module, 'validate', $controller))) {
                $validate = Loader::validate($controller);
                if (!$validate->check($data)) {
                    return ajax_return_adv_error($validate->getError());
                }
            }
            // 更新数据
            //dump($data);exit;
            Db::startTrans();
            try {
            	
            	$info['cancel_status'] = $data['cancel_status'];
            	
                $res = Db::table("tp_cancelinfo")->where(" id = ".$data['id'])->update($info);
                if($res && $info['cancel_status'] == 1){
      	          	$user_id = $data['user_id'];
                	$max_people = $data['max_people'];
                	$deposit = $data['deposit'];
                	$pay_money = $max_people*$deposit;//发起人发布活动押金
                	$user_money = Db::table('tp_user')->field('money')->where(" id = ".$user_id)->find();
                	$money = $user_money['money'];//发布人余额
                	$fan_money = $pay_money+$money;

                	Db::table('tp_user')->where(" id = ".$user_id)->setField('money',$fan_money);//将押金返还到活动发起人的账户
                	
                	$Credit = DIE_CREDIT;
			        Db::table('tp_user')->where('id',$user_id)->setDec('Credit',$Credit);
                }
                
                // 提交事务
                Db::commit();
				
                return ajax_return_adv("编辑成功");
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return ajax_return_adv_error($e->getMessage());
            }
        } else {
            // 编辑
            $id = $rId = $this->request->param('id');
            if (!$id) {
                throw new Exception("缺少参数ID");
            }
        	$field = "i.user_id,i.max_people,i.deposit,i.content,i.photo,i.address,i.create_time,i.go_time,i.cate_id,c.id,c.create_time as c_time,c.cancel_status,c.reason";
            $vo = $this->getModel($controller)->alias('c')
            	  ->join('tp_info i','i.id = c.active_id','left')
            	  ->field($field)
            	  ->find($rId);
            	  
			$vo['create_time'] = date('Y-m-d h:i:s',$vo['create_time']);
			$vo['go_time'] = date('Y-m-d h:i:s',$vo['go_time']);
			$vo['c_time'] = date('Y-m-d h:i:s',$vo['c_time']);
            if (!$vo) {
                throw new HttpException(404, '该记录不存在');
            }
            $this->view->assign("vo", $vo);

            return $this->view->fetch();
        }
    }
	




}

